package com.etc.service.impl;

import com.etc.dao.UserMapper;
import com.etc.model.Result;
import com.etc.model.User;
import com.etc.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;



@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public Result findByUsername(User user, HttpServletRequest request) {
        Result<User> result = new Result<>();
        HttpSession session = request.getSession();
        String randomString = (String) session.getAttribute("randomString");
        if (randomString != null && !"".equals(randomString)){
            if (randomString.equalsIgnoreCase(user.getVerify())){
                //判断user 用户名 密码 验证码 是否为空
                if(user == null || "".equals(user.getUsername()) || "".equals(user.getPassword())){
                    result.setStatus(400);
                    result.setMessage("用户名或者密码为空");
                }
                //根据用户名查询
                List<User> byUsername = userMapper.findByUsername(user.getUsername());
                //判断list集合是否为空并且这个集合里面是否有参数
                if(byUsername != null && !byUsername.isEmpty()) {
                    //拿出下标为0的用户赋给user1
                    User user1 = byUsername.get(0);
                    //判断页面传过来的参数跟数据库里面的数据是否一致
                    if (user1.getPassword().equals(user.getPassword())) {
                        result.setStatus(200);
                        result.setMessage("success");
                        result.setData(user1);
                    }else {
                        result.setStatus(500);
                        result.setMessage("密码错误");
                    }
                }
            }else {
                result.setStatus(600);
                result.setMessage("验证码错误");
            }
        }
        return result;
    }

    @Override
    public List<User> userByAll() {
        return null;
    }


}
